/**
 * 
 */
package vue;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

import controller.IController;
import controller.ISessionManager;

/**
 * A component use to render a custom {@link JTabbedPane}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGTabPanel extends JPanel implements MouseListener, ActionListener {

	private static final long	serialVersionUID	= 1L;
	public static final String	CLOSE_TOOLTIP		= "Clic to close the session";
	public static final String	ITEM_NEW			= "New CNF";
	public static final String	ITEM_CLOSE			= "Close";

	public static final int		SIZE_BUTTON			= 13;

	private String				name;
	private IController			controller;
	private ISessionManager		manager;

	private JLabel				nameLabel;
	private JButton				close;

	private JPopupMenu			rightMenu;
	private JMenuItem			closeItem;
	private JMenuItem			newItem;

	/**
	 * Create a {@link IGTabPanel}.
	 * 
	 * @param name
	 *            name of the tab
	 * @param controller
	 *            {@link IController} of the application.
	 * @param manager
	 *            {@link ISessionManager} of to the session associate to the tab.
	 */
	public IGTabPanel(String name, IController controller, ISessionManager manager) {
		this.name = name;
		this.manager = manager;
		this.controller = controller;

		nameLabel = new JLabel(this.name);
		add(nameLabel);

		close = new JButton() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void paint(Graphics g) {
				super.paint(g);
				g.drawLine(0, 0, getWidth(), getHeight());
				g.drawLine(0, getHeight(), getWidth(), 0);
			}

		};
		close.setPreferredSize(new Dimension(SIZE_BUTTON, SIZE_BUTTON));
		close.setToolTipText(CLOSE_TOOLTIP);
		// setContentAreaFilled(false);
		close.addActionListener(this);

		add(close);

		creatPopup();

		setOpaque(false);
		// addMouseListener(this);
		setSize(getPreferredSize());
		setVisible(true);
	}

	@Deprecated
	private void creatPopup() {
		rightMenu = new JPopupMenu();

		// new
		newItem = new JMenuItem(ITEM_NEW);
		newItem.addActionListener(this);
		rightMenu.add(newItem);

		rightMenu.addSeparator();

		// close
		closeItem = new JMenuItem(ITEM_CLOSE);
		closeItem.addActionListener(this);
		rightMenu.add(closeItem);
		// setComponentPopupMenu(rightMenu);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == close || e.getSource() == closeItem)
		{// demande close
			controller.closeSession(manager);
			return;
		}
		if (e.getSource() == newItem)
		{// open new cnf
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(null);

			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				File file = fc.getSelectedFile();
				controller.openSession(file);
			}
			return;
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// repaint();
		e.setSource(getParent());
		getParent().dispatchEvent(e);
		// e.translatePoint(-e.getX() - 1, -e.getY() - 1);
		disableEvents(MouseEvent.MOUSE_EVENT_MASK ^ MouseEvent.BUTTON2);

	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}

}
